<?php
	function habilitation()
	{
		$hab = array(
			1,
			2
		);
		
		if (in_array(get_s('bo_user_role'), $hab))
			return true;
		return false;
	}
	
	function ajout($table)
	{
		if (habilitation())
		{
			$retour = array();
			$message = '';
			$champs = champs();
			
			$php = '$x = 15;';
			$retour["php"] = $php;
			
			foreach ($champs as $champ)
			{
				$val = $_POST[$champ["id"]];
				if ($champ["obligatoire"] && !strlen($val))
					$message .= 'Le champ <strong>' . trad($champ["Affichage"], APPLNG) . '</strong> est vide mais obligatoire.<br />';
			}
			
			if (strlen($message))
			{
				$retour["statut"] = false;
				$retour["message"] = $message;
			}
			else
			{
				$gp_fonc = GP_fonctions::getInstance();
				$gp_db = GP_db::getInstance(BDD_DSN, BDD_UTILISATEUR, BDD_MOT_DE_PASSE);
				GP_ActiveRecord::setDefaultDBConnection($gp_db);
				
				$now = date("Y-m-d H:i:s");
				$add = new GP_ActiveRecord($table);
				$add -> datecrea = $now;
				$add -> datemodif = $now;
				foreach ($champs as $champ)
				{
					$val = $_POST[$champ["id"]];
					if ($champ["type"] == 'password')
						$val = md5($val);
					$add -> $champ["id"] = $val;
				}
				
				$add -> soumettre();
				
				$retour["statut"] = true;
			}
			return $retour;
		}
		return false;
	}
	
	function champs()
	{
		$retour = array();
		
				$retour["date_track"]["id"] = "date_track";
		$retour["date_track"]["Affichage"] = "Date";
		$retour["date_track"]["type"] = "date";
		$retour["date_track"]["max"] = 255;
		
		$retour["date_track"]["defaut"] = "";
		$retour["date_track"]["js"] = "return false;";
		$retour["date_track"]["obligatoire"] = true;
		$retour["date_track"]["tri"] = true;
		$retour["date_track"]["filtre"] = true;

		$retour["ip"]["id"] = "ip";
		$retour["ip"]["Affichage"] = "Adresse IP";
		$retour["ip"]["type"] = "input";
		$retour["ip"]["max"] = 255;
		
		$retour["ip"]["defaut"] = "";
		$retour["ip"]["js"] = "return false;";
		$retour["ip"]["obligatoire"] = true;
		$retour["ip"]["tri"] = true;
		$retour["ip"]["filtre"] = true;

		$retour["page_track"]["id"] = "page_track";
		$retour["page_track"]["Affichage"] = "Page";
		$retour["page_track"]["type"] = "input";
		$retour["page_track"]["max"] = 255;
		
		$retour["page_track"]["defaut"] = "";
		$retour["page_track"]["js"] = "return false;";
		$retour["page_track"]["obligatoire"] = true;
		$retour["page_track"]["tri"] = true;
		$retour["page_track"]["filtre"] = true;

		$retour["language"]["id"] = "language";
		$retour["language"]["Affichage"] = "Langue";
		$retour["language"]["type"] = "input";
		$retour["language"]["max"] = 255;
		
		$retour["language"]["defaut"] = "";
		$retour["language"]["js"] = "return false;";
		$retour["language"]["obligatoire"] = true;
		$retour["language"]["tri"] = true;
		$retour["language"]["filtre"] = true;

		$retour["country"]["id"] = "country";
		$retour["country"]["Affichage"] = "Pays";
		$retour["country"]["type"] = "input";
		$retour["country"]["max"] = 255;
		
		$retour["country"]["defaut"] = "";
		$retour["country"]["js"] = "return false;";
		$retour["country"]["obligatoire"] = true;
		$retour["country"]["tri"] = true;
		$retour["country"]["filtre"] = true;

		$retour["abbrevcountry"]["id"] = "abbrevcountry";
		$retour["abbrevcountry"]["Affichage"] = "Abbréviation pays";
		$retour["abbrevcountry"]["type"] = "input";
		$retour["abbrevcountry"]["max"] = 255;
		
		$retour["abbrevcountry"]["defaut"] = "";
		$retour["abbrevcountry"]["js"] = "return false;";
		$retour["abbrevcountry"]["obligatoire"] = true;
		$retour["abbrevcountry"]["tri"] = true;
		$retour["abbrevcountry"]["filtre"] = true;

		$retour["city"]["id"] = "city";
		$retour["city"]["Affichage"] = "Ville";
		$retour["city"]["type"] = "input";
		$retour["city"]["max"] = 255;
		
		$retour["city"]["defaut"] = "";
		$retour["city"]["js"] = "return false;";
		$retour["city"]["obligatoire"] = true;
		$retour["city"]["tri"] = true;
		$retour["city"]["filtre"] = true;

		$retour["coordinates"]["id"] = "coordinates";
		$retour["coordinates"]["Affichage"] = "Coordonnées";
		$retour["coordinates"]["type"] = "input";
		$retour["coordinates"]["max"] = 255;
		
		$retour["coordinates"]["defaut"] = "";
		$retour["coordinates"]["js"] = "return false;";
		$retour["coordinates"]["obligatoire"] = true;
		$retour["coordinates"]["tri"] = true;
		$retour["coordinates"]["filtre"] = true;

		$retour["os"]["id"] = "os";
		$retour["os"]["Affichage"] = "OS";
		$retour["os"]["type"] = "input";
		$retour["os"]["max"] = 255;
		
		$retour["os"]["defaut"] = "";
		$retour["os"]["js"] = "return false;";
		$retour["os"]["obligatoire"] = true;
		$retour["os"]["tri"] = true;
		$retour["os"]["filtre"] = true;

		$retour["browser"]["id"] = "browser";
		$retour["browser"]["Affichage"] = "Navigateur";
		$retour["browser"]["type"] = "input";
		$retour["browser"]["max"] = 255;
		
		$retour["browser"]["defaut"] = "";
		$retour["browser"]["js"] = "return false;";
		$retour["browser"]["obligatoire"] = true;
		$retour["browser"]["tri"] = true;
		$retour["browser"]["filtre"] = true;

		$retour["referer"]["id"] = "referer";
		$retour["referer"]["Affichage"] = "Référant";
		$retour["referer"]["type"] = "input";
		$retour["referer"]["max"] = 255;
		
		$retour["referer"]["defaut"] = "";
		$retour["referer"]["js"] = "return false;";
		$retour["referer"]["obligatoire"] = true;
		$retour["referer"]["tri"] = true;
		$retour["referer"]["filtre"] = true;



		return $retour;
	}
	
	$message_ok = false;
	if ($this -> action_form == 1)
	{
		$form = ajout($this -> table);
		if (!$form["statut"])
		{
			$message_erreur = $form["message"];
			$this -> setvar('message_erreur', $message_erreur);
		}
		else
			$message_ok = true;
	}
	
	$this -> setvar('message_ok', $message_ok);
	$this -> setvar('titre', 'Ajouter un track');
	$this -> setvar('retour_url', rw(''));
	$this -> setvar('habilitation', habilitation());
	$this -> setvar('champs', champs());
?>